package com.xmd.red.dao;

import com.alibaba.fastjson.JSON;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.xmd.red.bean.RedPacket;
import com.xmd.red.config.WxConfig;
import com.xmd.red.utils.RedPacketPool;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.Morphia;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by superbone on 2016/10/31.
 */
@Repository
public class RedPacketDao {

    private Datastore datastore;
    @Resource
    private WxConfig wxConfig;
    private List<Integer> redPool = new ArrayList<Integer>();

    private Logger log = LoggerFactory.getLogger(RedPacketDao.class);

    @PostConstruct
    public void init(){


        JSON.toJSONString(wxConfig);

        MongoClientURI clientURI = new MongoClientURI(wxConfig.getMongoUrl());
        MongoClient client = new MongoClient(clientURI);
        Morphia morphia = new Morphia();
        morphia.mapPackage("com.xmd");
        datastore = morphia.createDatastore(client, "redPacket");
        datastore.ensureIndexes();

        RedPacketPool.init();
        Map<Integer, Integer> pool = RedPacketPool.getPool();
        for(Map.Entry<Integer,Integer> entry : pool.entrySet()){
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            for(int i=0;i<value;i++){
                redPool.add(key);
            }
        }

    }

    public void save(RedPacket redPacket){
        datastore.save(redPacket);
    }

    public int getRandom(){
        Random random = new Random();
        int index = random.nextInt(redPool.size());
        return redPool.get(index);
    }

    public RedPacket queryByDeviceId(String deviceId){

       List<RedPacket> list = datastore.createQuery(RedPacket.class)
               .field(deviceId).equal(deviceId).asList();
        if(list.size()!=1){
            return null;
        }else{
            return list.get(0);
        }

    }

    public Iterable<Key<RedPacket>> save(List<RedPacket> reds){
        return datastore.save(reds);
    }

    public List<RedPacket> findNeedSend(Date date){

        List<Integer> states = new ArrayList<Integer>();
        states.add(0);
        states.add(1);
        return datastore.createQuery(RedPacket.class)
                .field("state").in(states)
                .field("createTime").greaterThan(date)
                .limit(1)
                .asList();

    }

    public int queryDeviceCount(String deviceId){

        return (int)datastore.createQuery(RedPacket.class)
                .field("deviceId").equal(deviceId)
                .countAll();
    }

    public List<RedPacket> queryDeviceIdList(String deviceId){
        return datastore.createQuery(RedPacket.class)
                .field("deviceId").equal(deviceId)
                .asList();
    }

    public int queryOpenIdCount(String openId){
        return (int)datastore.createQuery(RedPacket.class)
                .field("openId").equal(openId)
                .countAll();
    }
}
